package com.songtech.uitest.module.screen;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.AlertHandler;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.resources.IndentResource;
import com.songtech.uitest.resources.ThirdResource;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class ScreenTower {

    @Autowired
    @Qualifier("indentResource")
    private IndentResource indentResource;

    @Autowired
    private ThirdResource thirdResource;

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private StepParser stepParser;

    @Autowired
    private AlertHandler alertHandler;

    private Logger logger = LoggerFactory.getLogger(ScreenTower.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public void screenTower(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "接口模拟", "筛查结果数据模拟", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        logger.info(space.getSpace() + "开始填写站址筛查表单");

        //填表单
        driver.findElement(By.id("demandCreateTime")).click();
        driver.findElement(By.id("province")).clear();
        driver.findElement(By.id("province")).sendKeys(indentResource.getScreenProvince());
        driver.findElement(By.id("region")).clear();
        driver.findElement(By.id("region")).sendKeys(indentResource.getScreenRegion());
        driver.findElement(By.id("county")).clear();
        driver.findElement(By.id("county")).sendKeys(indentResource.getScreenCountry());
        driver.findElement(By.id("towerOrderCode")).clear();
        driver.findElement(By.id("towerOrderCode")).sendKeys(indent.getTowerId());


        //是否共享
        Select sharingOldSite = new Select(driver.findElement(By.id("sharingOldSite")));
        sharingOldSite.selectByVisibleText(indentResource.getScreenSharingOldSite());

        driver.findElement(By.id("siteName")).clear();
        driver.findElement(By.id("siteName")).sendKeys(indentResource.getScreenSiteName());
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(indentResource.getScreenLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(indentResource.getScreenLatitude());

        Select buildType = new Select(driver.findElement(By.id("buildType")));
        buildType.selectByVisibleText(indentResource.getScreenBuildType());

        driver.findElement(By.id("initialScreenTime")).click();
        //移动是否使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(indentResource.getScreenExistCm());
        //站址涉及客户数
        driver.findElement(By.id("siteclientCount")).clear();
        driver.findElement(By.id("siteclientCount")).sendKeys(indentResource.getScreenSiteClientCount());


        driver.findElement(By.id("saveSet")).click();

        //完成后，设置当前站址类型
        indent.setCurrentBuildType(indentResource.getScreenResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "站址筛查表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(6000);

//        AlertHandler.checkAndGo(driver);  //可能是页面长了，有问题

        Thread.sleep(3000);

        tabHandler.closeAllTabs(driver);

    }


    public void screen3Tower(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //打开模拟页
        menuHandler.clickMenuItem(driver, "铁塔建设管理", "站址筛查编制", "");
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);


        //查询此单
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(6000);

        //拖动滚动条到最右方
        String js = "$(\"div.fixed-table-body\").scrollLeft(document.body.scrollWidth)";
        ((JavascriptExecutor) driver).executeScript(js);
        Thread.sleep(1000);
        driver.findElement(By.xpath("//button[text()='编制站址筛查']")).click();

        //转到三方站址筛查详细页
        Thread.sleep(5000);
        //关闭上一个标签页
        tabHandler.closeFirstTab(driver);
        //回到三方站址筛查详细页上
        WebElement frame = driver.findElements(By.name("iframe0")).get(1);
        driver.switchTo().frame(frame);

        Thread.sleep(1000);
        logger.info(space.getSpace() + "开始填写三方站址筛查表单");
        //填写表单
        //重置
        driver.findElement(By.cssSelector("button.btn.btn-info.btn-danger")).click();
        Thread.sleep(2000);
        driver.findElement(By.xpath("//button[text()='确定']")).click();
        Thread.sleep(1000);

        //是否共享
        Select sharingOldSite = new Select(driver.findElement(By.id("sharingOldSite")));
        sharingOldSite.selectByVisibleText(thirdResource.getScreenSharingOldSite()); //是，否
        //三方站址名称
        driver.findElement(By.id("siteName")).sendKeys(thirdResource.getScreenSiteName());
        driver.findElement(By.id("longitude")).clear();
        driver.findElement(By.id("longitude")).sendKeys(thirdResource.getScreenLongitude());
        driver.findElement(By.id("latitude")).clear();
        driver.findElement(By.id("latitude")).sendKeys(thirdResource.getScreenLatitude());

        //站址编码，随便写
        driver.findElement(By.id("siteAddressCode")).sendKeys("aabbcc");
        driver.findElement(By.id("antennaHeightDuration")).sendKeys("15~20");
        driver.findElement(By.id("antennaCount")).sendKeys("3");

        //建设方式：存量直接满足，新建，改造
        Select buildType = new Select(driver.findElement(By.id("buildType")));
        buildType.selectByVisibleText(thirdResource.getScreenBuildType());

        driver.findElement(By.id("initialScreener")).sendKeys("xh");

        driver.findElement(By.id("initialScreenTime")).click();
        Thread.sleep(500);

        //移动是否已使用站址
        Select existCm = new Select(driver.findElement(By.id("existCm")));
        existCm.selectByVisibleText(thirdResource.getScreenExistCm()); //是，否

        //站址涉及客户数,1,2,3
        Select siteClientCount = new Select(driver.findElement(By.id("siteclientCount")));
        siteClientCount.selectByVisibleText(thirdResource.getScreenSiteClientCount());


        driver.findElement(By.xpath("//button[text()='确认并提交']")).click();

        Thread.sleep(5000);

        alertHandler.checkAndGo(driver);

        //完成后，设置当前站址类型
        indent.setCurrentBuildType(thirdResource.getScreenResourceType());
        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        logger.info(space.getSpace() + "三方站址筛查表单提交完成，当前站址类型为" + indent.getCurrentBuildType());

        Thread.sleep(4000);

        tabHandler.closeAllTabs(driver);


    }


}
